package org.budo.activiti.form.type;

import java.util.LinkedHashMap;
import java.util.Map;

import org.activiti.bpmn.model.FormProperty;
import org.activiti.bpmn.model.FormValue;
import org.activiti.engine.form.AbstractFormType;
import org.activiti.engine.form.FormType;
import org.activiti.engine.impl.form.FormTypes;

import lombok.NoArgsConstructor;

/**
 * @author lmw
 */
@NoArgsConstructor
public class BudoFormTypeList extends FormTypes {
    public BudoFormTypeList(FormType[] formTypes) {
        for (FormType formType : formTypes) {
            this.addFormType((AbstractFormType) formType);
        }
    }

    @Override
    public AbstractFormType parseFormPropertyType(FormProperty formProperty) {
        if ("enum_vertical".equals(formProperty.getType())) {
            return this.enumFormTypeVertical(formProperty);
        }

        return super.parseFormPropertyType(formProperty);
    }

    private AbstractFormType enumFormTypeVertical(FormProperty formProperty) {
        Map<String, String> values = new LinkedHashMap<String, String>();
        for (FormValue formValue : formProperty.getFormValues()) {
            values.put(formValue.getId(), formValue.getName());
        }

        return new EnumFormTypeVertical(values);
    }
}